#!/usr/bin/env python
# encoding: utf-8

"""
@author: Cao
@contact: caoqiang0012@163.com
@file: SimpleSSH.py
@time: 23/05/2018 10:24 AM
"""
import paramiko

import json

class SimpleSSH:

    def __init__(self):

        self.ssh = paramiko.SSHClient()

    def connect(self, addr, username, password):
        self.address = addr
        self.username = username
        self.password = password
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
        self.ssh.connect(self.address, 22, self.username, self.password, look_for_keys=False)

    def enableBash(self):
        self.ssh.exec_command('source /robots/machine-global-cache-for-personal-service/bashrc')

    def getState(self):

        stdin, stdout, stderr = self.ssh.exec_command("robot-control status")
        lines = stdout.readlines()
        print(lines)
        # #state = lines[7][7:-1]
        # #return state

        dict = {}

        for i in range(1, 8):
            # print(lines[i])
            line = lines[i].strip('\n').split(": ")

            print(line)
            # print(type(line))
            # print(line[0])
            # dict[line[0]] = line[1]

        # return dict['State']

    def getRobotInfo(self):

        with open('../HostInfo/RobotsInfo.json', 'r') as f:
            data = json.load(f)
        return data

    def stopRobot(self):
        stdin, stdout, stderr = self.ssh.exec_command("robot-control stop")
        return stdout

    def startRobot(self):
        stdin, stdout, stderr = self.ssh.exec_command("robot-control start")
        return stdout

    def closeSession(self):
        self.ssh.close()